const PORT1 = "/api";
import {
	baseURL
} from "./env.js";


export const uModal = (title = "确定要删除吗?") =>
	new Promise((resolve, reject) =>
		uni.showModal({
			title,
			success(res) {
				resolve(res.confirm);
			},
			fail() {
				reject("error");
			},
		})
	);

export const uGetStorageSync = (key) => uni.getStorageSync(key);
export const uSetStorageSync = (key, value) => uni.setStorageSync(key, value);
export const uRemoveStorageSync = (key) => uni.removeStorageSync(key);

export const uToast = (
		title = "操作成功!",
		icon = "success",
		duration = 2000
	) =>
	new Promise((resolve, reject) =>
		uni.showToast({
			title,
			icon,
			duration,
			success() {
				resolve("success");
			},
			fail() {
				reject("error");
			},
		})
	);

export const uUploadImage = async () => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 1,
			success: (res) => {
				console.log("res.tempFilePaths", res.tempFilePaths);
				res.tempFilePaths.forEach((e) => {
					uni.uploadFile({
						url: baseURL + PORT1 + "/Tools/UploadFiles",
						filePath: e,
						name: "file",
						header: {
							"content-type": "multipart/form-data; charset=utf-8",
							Authorization: "Bearer " + uni.getStorageSync("token"),
							systemAppId:uni.getStorageSync('systemAppId')
						},
						success: (uploadFileRes) => {
							var jsonData = JSON.parse(uploadFileRes
								.data);
							if (jsonData.code == 0) {
								console.log("jsonData", jsonData);
								resolve(jsonData.data.src);
							} else {
								reject("图片上传失败");
							}
						},
						fail: (err) => {
							reject(err);
						},
					});
				});
				//this.imageList = this.imageList.concat(res.tempFilePaths);
			},
			fail: (err) => {
				console.log("err: ", err);
				// #ifdef APP-PLUS
				if (err["code"] && err.code !== 0 && this.sourceTypeIndex === 2) {
					this.checkPermission(err.code);
				}
				// #endif
				// #ifdef MP
				if (err.errMsg.indexOf("cancel") !== "-1") {
					return;
				}
				uni.getSetting({
					success: (res) => {
						let authStatus = false;
						switch (this.sourceTypeIndex) {
							case 0:
								authStatus = res.authSetting["scope.camera"];
								break;
							case 1:
								authStatus = res.authSetting["scope.album"];
								break;
							case 2:
								authStatus =
									res.authSetting["scope.album"] &&
									res.authSetting["scope.camera"];
								break;
							default:
								break;
						}
						if (!authStatus) {
							uni.showModal({
								title: "授权失败",
								content: "Hello uni-app需要从您的相机或相册获取图片，请在设置界面打开相关权限",
								success: (res) => {
									if (res.confirm) {
										uni.openSetting();
									}
								},
							});
						}
					},
				});
				// #endif
			},
		});
	});
};

export const uUploadFile = async (filePath) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseURL + PORT1 + "/Tools/UploadFiles",
			filePath,
			name: "file",
			header: {
				"content-type": "multipart/form-data; charset=utf-8",
				token: tokenBear,
			},
			success: (uploadFileRes) => {
				var jsonData = JSON.parse(uploadFileRes.data);
				if (jsonData.code == 0) {
					console.log("jsonData", jsonData);
					resolve(jsonData.data.src);
				} else {
					reject("文件上传失败");
				}
			},
			fail: (err) => {
				reject(err);
			},
		});
	});
};

export const uShowLoading = async (title = "加载中。。。") =>
	uni.showLoading({
		title,
	});

export const uHideLoading = async () => uni.hideLoading();

export const uNavigateTo = (url) =>
	uni.navigateTo({
		url,
	});

export const uReLaunch = (url) =>
	uni.reLaunch({
		url,
	});

export const uRedirectTo = (url) =>
	uni.redirectTo({
		url,
	});

export const uNavigateBack = (delta = 1) =>
	uni.navigateBack({
		delta,
	});

export const uSwitchTab = (url) =>
	uni.switchTab({
		url,
	});

export const uSetNavigationBarTitle = (title) =>
	uni.setNavigationBarTitle({
		title,
	});

export const uPreviewImage = (url) =>
	uni.previewImage({
		urls: [url],
	});

export const uScanCode = () =>
	new Promise((resolve, reject) => {
		uni.authorize({
			scope: "scope.camera",
			success() {
				uni.scanCode({
					success(res) {
						if (res.result) {
							// 扫描成功，处理二维码内容
							resolve(res.result);
							// 这里可以进行后续操作，如跳转页面、显示信息等
						} else {
							reject("授权失败！");
						}
					},
					fail() {
						// 调用相机失败
						uni.showToast({
							title: "调用相机失败",
							icon: "none",
						});
					},
				});
			},
			fail() {
				// 用户拒绝授权
				uni.showToast({
					title: "您拒绝了授权",
					icon: "none",
				});
				reject("您拒绝了授权");
			},
		});
	});

/**
 * 订阅推送消息
 * @param tmplIds 模板ID
 */
export async function uRequestSubscribeMessage(tmplIds = []) {
	return new Promise(
		(resolve, reject) => {
			uni.requestSubscribeMessage({
				tmplIds: tmplIds, // 数组，最多3个订阅
				success(res) {
					console.log("订阅消息结果", res);
					resolve(true)
					// 处理订阅消息的结果
				},
				error() {
					reject(false)
				},
			});
		}
	);
}